package com.example.server.aspect;

import com.example.server.connection.HaeConnection;
import com.example.server.trasaction.HaeTransactionManager;
import com.example.server.trasaction.HaeTransactionModel;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.sql.Connection;

/**
 * @Auther: yuanjianpeng
 * @Date: 2019/4/29 10:57
 * @Description:
 */
@Aspect
@Component
public class HaeDataSourceAspect {

    @Around("execution(* javax.sql.DataSource.getConnection(..))")
    public Connection around(ProceedingJoinPoint joinPoint) throws Throwable {

        //需要获取到HaeTransactionAspect中创建的HaeTransactionModel变量，因为在同一线程，所以共享
        HaeTransactionModel haeTransactionModel = HaeTransactionManager.getCurrent();
        //取得数据库连接信息
        Connection connection = (Connection) joinPoint.proceed();
        return new HaeConnection(connection, haeTransactionModel);
    }
}
